
const FairyGUI = CS.FairyGUI;
const FairyEditor = CS.FairyEditor
import UIWind, { WindData } from "./UIWind"
import ToolMgr from '../ToolMgr';
const App = FairyEditor.App;

export default class NodeThree extends UIWind {

    constructor() {
        super(new WindData("Extend", "NodeThree"))
    }

    static show() {
        let url = ToolMgr.joinUrl("Extend", "NodeThree")
        if (UIWind.FindUrl(url) == undefined) {
            UIWind.add(new this)
        }

        super.show(url)
    }


    private three: CS.FairyGUI.GTree;
    private close: CS.FairyGUI.GButton;

    private sousuo: CS.FairyGUI.GButton;
    private sousuo_input: CS.FairyGUI.GLabel;
    protected onInit(): void {
        super.onInit();
        this.three = this.contentPane.GetChild("three").asTree;
        this.close = this.contentPane.GetChild("close").asButton;
        this.sousuo = this.contentPane.GetChild("sousuo").asButton;
        this.sousuo_input = this.contentPane.GetChild("sousuo_input").asLabel;
        this.close.onClick.Add(this.onCancelClick.bind(this))

        this.three.treeNodeRender = this.renderTreeNode.bind(this);
        this.three.treeNodeWillExpand = this.onTreeNodeExpand.bind(this);
        this.sousuo.onClick.Add(this.onSousuoClick.bind(this))
    }

    protected onShown(): void {
        super.onShown();
        this.three.RemoveChildrenToPool();
        let rootNode = new CS.FairyGUI.GTreeNode(true)
        rootNode.data = App.groot;
        this.three.rootNode.AddChild(rootNode)
    }

    protected onHide(): void {
        super.onHide();
        this.three.RemoveChildrenToPool();
    }

    /**检索节点树 */
    private onSousuoClick() {
        let name = this.sousuo_input.GetChild("title").text
        if (name == "") return
        let target = ToolMgr.getChildToChild(App.groot, name);
        if (target) {
            ToolMgr.Log(`找到节点:${target.name}`)
            ToolMgr.Log("路径:" + ToolMgr.getAbsPath(target))
        }
    }

    private getchilds(obj: CS.FairyGUI.GComponent) {
        return ToolMgr.getCurrentParentChild(obj)
    }

    private updateThree(node: CS.FairyGUI.GTreeNode) {
        let arrray = this.getchilds(node.data)
        for (let i = 0; i < arrray.length; i++) {
            if(arrray[i].name == this.windData.resName) continue;
            let obj = arrray[i]
            let childNode: CS.FairyGUI.GTreeNode = null;
            childNode = new CS.FairyGUI.GTreeNode(this.isComponent(obj))
            childNode.data = obj
            node.AddChild(childNode)
        }
    }

    private renderTreeNode(node: CS.FairyGUI.GTreeNode, index: number, level: number): void {
        node.cell.text = `${node.data.name} ${(ToolMgr.objTextIsNull(node.data)?"":(" "+node.data.text))} (` + (node.data.GetType().ToString() + ")")
        node.cell.icon = ToolMgr.getIcon(node.data)
    }

    private onTreeNodeExpand(node: CS.FairyGUI.GTreeNode): void {
        if (node.expanded)
            this.updateThree(node)
        else {
            node.RemoveChildren()
        }
    }

    private onCancelClick() {
        let self = this;
        this.Hide();
        FairyGUI.Timers.inst.Add(0.05, 1, () => {
            UIWind.remove(self)
        })
    }

    //是否是组件
    private isComponent(obj: CS.FairyGUI.GObject): boolean {

        if (obj instanceof CS.FairyGUI.GComponent) {
            return true;
        } else if (obj instanceof CS.FairyGUI.GButton) {
            return true;
        } else if (obj instanceof CS.FairyGUI.GLabel) {
            return true;
        } else if (obj instanceof CS.FairyGUI.GProgressBar) {
            return true;
        } else if (obj instanceof CS.FairyGUI.GSlider) {
            return true;
        } else if (obj instanceof CS.FairyGUI.GComboBox) {
            return true;
        } else if (obj instanceof CS.FairyGUI.GScrollBar) {
            return true;
        }
        return false
    }
}

